import com.alibaba.fastjson.JSON;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.xxl.job.core.biz.model.ReturnT;
import executor.Entity.Testdata;
import executor.XxlJobExecutorApplication;
import executor.dao.TestdataDao;
import freemarker.template.Configuration;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = XxlJobExecutorApplication.class)
public class ListDemo {

    private Configuration configuration;
    protected static Statement statement = null;
    protected static String insertTable = "Testdata";

    @Test
    public  void test(){
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        URI uri = null;

        try {
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("flag", "4"));
            params.add(new BasicNameValuePair("meaning", "1"));
            //Token值
            String token = "db32a74e4d6593cb17bc9ec81d82ee6f4e3bf090b8d2db34e92f16b5e7d82f9a";
            //API路径
            uri = new URIBuilder().setScheme("https").setHost("tower.im/api/v1").setPath("/user").setParameters(params).build();
        }catch (URISyntaxException e1){
            e1.printStackTrace();
        }
        //请求头
        HttpGet httpGet = new HttpGet(uri);
        httpGet.addHeader("Content-type","application/json;charset=utf8");
        httpGet.addHeader("Host","<calculated when request is sent>");
        httpGet.addHeader("Authorization","Bearer 1ff76400a68d6bdcbd0b67c1a3a07b82200e4ed0e1d11869a24fc4ec6529128f");
        httpGet.addHeader("Cookie","uid=rBACIl7DTBnAfRKgWiALAg==; _ga=GA1.2.567674898.1589857720; intercom-id-xbtsuf77=e783b7c4-de5e-494b-a6c9-f45d78baac5e; mp_72ddb7dd79e1c48afb3e27e0efd70a74_mixpanel=%7B%22distinct_id%22%3A%20%221722ae795434d9-037e3d871b94f3-26096c2f-144000-1722ae7954448e%22%2C%22%24device_id%22%3A%20%221722ae795434d9-037e3d871b94f3-26096c2f-144000-1722ae7954448e%22%2C%22%24initial_referrer%22%3A%20%22%24direct%22%2C%22%24initial_referring_domain%22%3A%20%22%24direct%22%7D; remember_token=63009470-4880-4bb8-adfe-0581ca97fac7; remember_team_guid=66f9f25eee0a44d18282ffdcd0b33e1f; _tower2_session=1b9090567d7db90a5ba900dfeba4d606; Hm_lvt_e1fa918d304786452f7d6936febd93c6=1589966127,1590134154,1590197438,1590732580; Hm_lpvt_e1fa918d304786452f7d6936febd93c6=1590732580; _gid=GA1.2.994055032.1590732580");

        //响应模型
        CloseableHttpResponse response = null;
        try {
            //由客户端执行（发送）Get请求
            response = httpClient.execute(httpGet);
            //从响应模型中获取响应实体并存入数据库
            HttpEntity responseEntity = response.getEntity();
            final String data = EntityUtils.toString(responseEntity);
            String s = data;
            Testdata testdata = new Testdata();
            testdata.setResultData(data);
            testdata.setCreateTime(new Date());
            if (responseEntity!=null){
                System.out.println("======响应内容长度为:"+responseEntity.getContentLength());
                System.out.println("======响应内容为:"+ data);
                System.out.println("以下数据已存入数据库并发送到钉钉群:"+data);
            }
        }catch (ClientProtocolException e){
            e.printStackTrace();
        }catch (ParseException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                //释放资源
                if (httpClient!=null){
                    httpClient.close();
                }
                if (response!=null){
                    response.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        System.out.println("以上为测试数据，Token刷新时间为两小时。");
    }

    public  void save(String data){

    }
    }
